﻿using System;
using eCommerce.core;
using eCommerce.db.BORespository;

namespace eCommerce.web.Controls.Sidebar
{
    public partial class Category : System.Web.UI.UserControl
    {
        public int TypeId           { get; set; }
        public int ParentCategoryId { get; set; }        
        public int CategoryId       { get; set; }        
        public string CategoryName  { get; set; }
        public string ParentCategoryName { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CategoryId > 0 && TypeId > 0)
                {
                    Page.RegisterHiddenField("hidCategoryId", CategoryId.ToString());
                    CategoryId = ParentCategoryId == 0 ? CategoryId : ParentCategoryId;
                    
                    var lstCategory = CategoryBo.GetListByParentId(TypeId, CategoryId, (int)CategoryType.Active);
                    ltCategory.Text = (!string.IsNullOrEmpty(ParentCategoryName)) ?  ParentCategoryName : CategoryName;
                    if (lstCategory.Count > 0)
                    {
                        rptCategory.DataSource = lstCategory;
                        rptCategory.DataBind();
                    }                  
                }
                else switch (CategoryName.ToLower())
                {
                    case "tin-tuc":
                        {
                            var lstCategory = CategoryBo.GetListByParentId((int) CategoryType.Article, (int) CategoryType.Root, (int)CategoryType.Active);
                            ltCategory.Text = "Chuyên mục";
                            if (lstCategory.Count > 0)
                            {
                                rptCategory.DataSource = lstCategory;
                                rptCategory.DataBind();
                            }                  
                        }
                        break;
                    case "san-pham":
                        {
                            var lstCategory = CategoryBo.GetListByParentId((int)CategoryType.Product, (int)CategoryType.Root, (int)CategoryType.Active);
                            ltCategory.Text = "Danh mục";
                            if (lstCategory.Count > 0)
                            {
                                rptCategory.DataSource = lstCategory;
                                rptCategory.DataBind();
                            } 
                        }
                        break;
                }
            }
        }
    }
}